<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入套餐名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择适合性别" clearable>
          <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入套餐编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="" prop="helpCode">
        <el-input v-model="queryParams.helpCode" placeholder="请输入助记码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="" prop="isSubscribe">
        <el-select v-model="queryParams.isSubscribe" placeholder="请选择是否上架" clearable>
          <el-option v-for="dict in dict.type.sys_putaway" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['jktj:setmeal:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['jktj:setmeal:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['jktj:setmeal:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['jktj:setmeal:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="setmealList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="setmId" /> -->
      <!-- <el-table-column label="图片" align="center" prop="img" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="套餐编号" align="center" prop="code" /> -->
      <el-table-column label="套餐名称" align="center" prop="name" width="200">
        <template slot-scope="scope">
          <router-link :to="'/jktj/checkgroup/index/' + scope.row.setmId" class="link-type">
            <span>{{ scope.row.name }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column label="适合性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="适合年龄" align="center" prop="age" />
      <el-table-column label="套餐价格" align="center" prop="price" />
      <!-- <el-table-column label="注意事项" align="center" prop="attention" /> -->


      <el-table-column label="助记码" align="center" prop="helpCode" />
      <el-table-column label="是否上架" align="center" prop="isSubscribe" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_putaway" :value="scope.row.isSubscribe" />
        </template>
        <!-- <template slot-scope="scope">
          <el-switch v-model="scope.row.isSubscribe" active-value="1" inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template> -->
      </el-table-column>
      <el-table-column label="备注说明" align="center" prop="remark" width="200" />
      <el-table-column label="套餐数量" align="center" prop="stemNumer" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button size="small" type="primary" icon="el-icon-sell" @click="handleUpdate(scope.row)"
            v-hasPermi="['jktj:setmeal:edit']">上架</el-button>
          <el-button size="small" type="warning" icon="el-icon-sold-out" @click="handleDelete(scope.row)"
            v-hasPermi="['jktj:setmeal:edit']">下架</el-button>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="170">
        <template slot-scope="scope">
          <el-button size="text" type="primary" icon="el-icon-plus" @click="handlePutaway(scope.row)"
            v-hasPermi="['jktj:setmeal:edit']">上架</el-button>
          <el-button size="text" type="warning" icon="el-icon-minus" @click="handleSoldOut(scope.row)"
            v-hasPermi="['jktj:setmeal:edit']">下架</el-button>
          <el-button size="small" type="success" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['jktj:setmeal:edit']">修改</el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['jktj:setmeal:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改体检套餐对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="640px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="图片" prop="img">
          <image-upload v-model="form.img" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="套餐编号" prop="code">
              <el-input v-model="form.code" placeholder="请输入套餐编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入套餐名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="适合性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择适合性别">
                <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="适合年龄" prop="age">
              <el-input v-model="form.age" placeholder="请输入适合年龄" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="助记码" prop="helpCode">
              <el-input v-model="form.helpCode" placeholder="请输入助记码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐价格" prop="price">
              <el-input :disabled="true" v-model="form.price" placeholder="请输入价格" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否上架" prop="isSubscribe">
              <el-select v-model="form.isSubscribe" placeholder="请选择是否上架">
                <el-option v-for="dict in dict.type.sys_putaway" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="套餐数量" prop="stemNumer">
              <el-input v-model="form.stemNumer" placeholder="请输入套餐数量" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="注意事项" prop="attention">
          <el-input v-model="form.attention" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSetmeal, getSetmeal, delSetmeal, addSetmeal, updateSetmeal } from "@/api/jktj/setmeal";
import { listReserve, getReserve, delReserve, addReserve, updateReserve } from "@/api/jktj/reserve";

export default {
  name: "Setmeal",
  dicts: ['subscribe_yes_no', 'sys_user_sex', 'sys_putaway'],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 体检套餐表格数据
      setmealList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sex: null,
        attention: null,
        code: null,
        helpCode: null,
        isSubscribe: null,
      },
      // 表单参数
      form: {},
      // 体检套餐预约表单
      reserveForm: {
        id: null,
        setmId: null,
        setmPrice: null,
        userName: null,
        resDatetime: null,
        setmResidue: null,
        isReserve: null,
        audit: null,
        tjRoute: null,
        tjType: null,
        tjUser: null,
        isSubscribe: null,
        userPhone: null,
        resNumber: 1,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null

      },
      // 表单校验
      rules: {
      }
    };
  },
  created () {
    this.getList();
  },
  methods: {
    /** 查询体检套餐列表 */
    getList () {
      this.loading = true;
      listSetmeal(this.queryParams).then(response => {
        this.setmealList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        setmId: null,
        name: null,
        sex: null,
        age: null,
        price: null,
        attention: null,
        code: null,
        img: null,
        helpCode: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isSubscribe: null,
        stemNumer: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.setmId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    // 体检套餐上架
    handlePutaway (row) {
      if (row.isSubscribe == 1) {
        this.$modal.msgError("该套餐已上架！请先下架再重新上架。");
        return;
      }
      console.log(1111)
      this.form = row;
      this.form.isSubscribe = 1;
      updateSetmeal(this.form).then(response => {
        this.$modal.msgSuccess("套餐上架成功!");
      });
      // this.reserveForm = row;
      this.reserveForm.isSubscribe = 1;
      this.reserveForm.setmId = row.setmId;
      this.reserveForm.setmPrice = row.price;
      this.reserveForm.setmResidue = row.stemNumer;
      this.reserveForm.isSubscribe = 1;
      this.reserveForm.isReserve = 5;
      this.reserveForm.audit = 4;
      addReserve(this.reserveForm).then(response => {
        console.log(this.reserveForm)
      });
    },
    // 体检套餐下架
    handleSoldOut (row) {
      if (row.isSubscribe == 0) {
        this.$modal.msgError("该套餐已下架!");
        console.log("该套餐已下架");
        return;
      }
      console.log(222)
      this.reserveForm = row;
      this.reserveForm.isSubscribe = 0;
      updateSetmeal(this.reserveForm).then(response => {
        this.$modal.msgSuccess("套餐下架成功!");
      });
    },

    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加体检套餐";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const setmId = row.setmId || this.ids
      getSetmeal(setmId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改体检套餐";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.setmId != null) {
            updateSetmeal(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });

          } else {
            this.form.isSubscribe = 0;
            addSetmeal(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
            // 
            // this.reserveForm.setmId = this.form.setmId;
            // this.reserveForm.setmPrice = this.form.price;
            // this.reserveForm.setmResidue = this.form.stemNumer;
            // this.reserveForm.isSubscribe = 0;
            // this.reserveForm.isReserve = 5;
            // this.reserveForm.audit = 4;
            // addReserve(this.reserveForm).then(response => {
            //   console.log(this.reserveForm)
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const setmIds = row.setmId || this.ids;
      this.$modal.confirm('是否确认删除该体检套餐？').then(function () {
        return delSetmeal(setmIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('jktj/setmeal/export', {
        ...this.queryParams
      }, `setmeal_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
